import { Controller } from "../../mvc/core/Controller";
import { DataEvent } from "../events/DataEvent";
import { MsgManager } from "../manager/MsgManager";
import { Language } from "../../../extend/language/Language";
import { ItemData } from "../resource/info/ItemData";
import { HTMLUtil } from "../../../com/gengine/utils/HTMLUtil";
import { ColorConfig } from "../resource/ColorConfig";
import { Alert } from "../../../com/mui/controls/Alert";
import { AIManager } from "../scene/ai/AIManager";
import { ENpcEffect } from "../../../Message/Public/ENpcEffect";
import { PulseSharedObject } from "../view/player/PulseSharedObject";
import { TodayNoTipsConst } from "../../component/gconst/TodayNoTipsConst";
import { GlobalStyle } from "../../common/global/GlobalStyle";
import { CheckBoxWin } from "../view/common/alertwins/CheckBoxWin";
import { GameProxy } from "../mvc/GameProxy";
import { ClockManager } from "../manager/ClockManager";
import { GameConst } from "../../component/gconst/GameConst";
import { Global } from "../../../com/gengine/global/Global";
import { QuickBuyWindow } from "../view/common/QuickBuyWindow";
import { ShopItemInfo } from "../resource/info/ShopItemInfo";
import { ShopConfig } from "../resource/ShopConfig";
import { ECategory } from "../../../Message/Public/ECategory";
import { EProp } from "../../../Message/Public/EProp";
import { ETaskGroup } from "../../../Message/Game/ETaskGroup";
import { ETaskStatus } from "../../../Message/Game/ETaskStatus";
import { Dispatcher } from "../../mvc/core/Dispatcher";
import { EventName } from "../mvc/EventName";
import { IKillEvilTaskModule } from "../../../modules/interfaces/IKillEvilTaskModule";
import { MessageBlock } from "../../../Framework/MQ/MessageBlock";
import { SKillEvilReleaseInfo } from "../../../Message/Game/SKillEvilReleaseInfo";
import { SKillEvilCanGetInfo } from "../../../Message/Game/SKillEvilCanGetInfo";
import { SMyKillEvilGetInfo } from "../../../Message/Game/SMyKillEvilGetInfo";
import { SSystemGetTaskIssue } from "../../../Message/Game/SSystemGetTaskIssue";
import { KillEvilTaskHintIcon } from "../view/delegateTask/KillEvilTaskHintIcon";
import { IView } from "../../mvc/interfaces/IView";
import { KillEvilTaskModule } from "../../../modules/KillEvilTaskModule";
import { WindowEvent } from "../../component/window/WindowEvent";
import { GModuleEvent } from "../mvc/GModuleEvent";
import { NetDispatcher } from "../../mvc/core/NetDispatcher";
import { EGateCommand } from "../../../Message/Command/EGateCommand";
import { ServerCommand } from "../mvc/ServerCommand";
type int = number;
//class KillEvilTaskController
    
    export  class KillEvilTaskController extends Controller
    {
       

        /* internal  */onOpenKillEvilTaskWin(arg1: DataEvent): void
        {
            if (cache.role.roleInfo.level < 40) 
            {
                MsgManager.showRollTipsMsg(Language.getString(69008));
                return;
            }
            view.show();
            return;
        }

        /* internal  */onKillEvilTaskPropUse(arg1: DataEvent): void
        {
            var e: DataEvent;
            var prop: ItemData;
            var htmltext: string;

            var loc1;
            e = arg1;
            if (cache.role.roleInfo.level < 40) 
            {
                MsgManager.showRollTipsMsg(Language.getString(69008));
                return;
            }
            prop = e.data as ItemData;
            htmltext = Language.getStringByParam(69011, HTMLUtil.addColor(prop.itemInfo.item.name, ColorConfig.instance.getItemColor(prop.itemInfo.item.color).color), HTMLUtil.addColor(Language.getString(69009), "#00FF00"), HTMLUtil.addColor(Language.getString(69010), "#00FF00"));
            Alert.okLabel = Language.getString(69012);
            Alert.buttonWidth = 75;
            Alert.show(htmltext, Language.getString(69900), Alert.OK | Alert.CANCEL, null, (arg1: int): void
            {
                if (arg1 == Alert.OK) 
                {
                    AIManager.onAutoPathAIControl(cache.scene.getNpcByEffect(ENpcEffect._ENpcEffectEvilTask));
                }
                return;
            })
            return;
        }

        public onPublishKillEvilTask(arg1: DataEvent): void
        {
            var loc3=null;
            var loc4=null;
            var loc1=arg1.data as int;
            if (!this.checkIsCanReleaseTask(loc1)) 
            {
                return;
            }
            var loc2=PulseSharedObject.isTodayNotTips(TodayNoTipsConst.KillEvilTaskPublishDaily);
            if (loc2) 
            {
                this.onReleaseTaskAlert(Alert.OK, loc1, false);
            }
            else 
            {
                loc3 = ColorConfig.instance.getItemColor(cache.killEvilTask.getKillEvilTaskColor(loc1));
                loc4 = HTMLUtil.addColor(Language.getString(69013), GlobalStyle.color1) + HTMLUtil.addColor(cache.killEvilTask.getKillEvilTaskName(loc1), loc3.color) + HTMLUtil.addColor(Language.getStringByParam(69014, cache.killEvilTask.getExpByType(loc1)), GlobalStyle.color7) + HTMLUtil.addColor(Language.getStringByParam(69015, cache.killEvilTask.releaseInfo.canReleaseCount), GlobalStyle.color6);
                Alert.buttonWidth = 75;
                Alert.alertWinRenderer = CheckBoxWin;
                Alert.extendObj = loc1;
                Alert.okLabel = Language.getString(69016);
                Alert.calcelLabel = Language.getString(69017);
                Alert.show(loc4, Language.getString(69002), Alert.OK | Alert.CANCEL, null, this.onReleaseTaskAlert);
            }
            return;
        }

        /* internal  */onReleaseTaskAlert(arg1: int, arg2: int, arg3: Boolean): void
        {
            if (arg1 == Alert.OK) 
            {
                GameProxy.killEvilTask.releaseKillEvilTask(arg2);
            }
            if (arg3) 
            {
                PulseSharedObject.save(TodayNoTipsConst.KillEvilTaskPublishDaily, true);
            }
            return;
        }

        /* internal  */checkIsCanReleaseTask(arg1: int): Boolean
        {
            var loc2=null;
            var loc3=null;
            var loc4=null;
            var loc1=ClockManager.instance.nowDate;
            if ((loc1.hours < GameConst.ReleaseKillEvilTaskTime_start || loc1.hours == GameConst.ReleaseKillEvilTaskTime_start && loc1.minutes < 30 || loc1.hours > GameConst.ReleaseKillEvilTaskTime_end || loc1.hours == GameConst.ReleaseKillEvilTaskTime_end && loc1.minutes > 30) && !Global.isDebugModle) 
            {
                loc2 = GameConst.ReleaseKillEvilTaskTime_start < 10 ? "0" + GameConst.ReleaseKillEvilTaskTime_start : "" + GameConst.ReleaseKillEvilTaskTime_start;
                loc3 = GameConst.ReleaseKillEvilTaskTime_end < 10 ? "0" + GameConst.ReleaseKillEvilTaskTime_end : "" + GameConst.ReleaseKillEvilTaskTime_end;
                MsgManager.showRollTipsMsg(Language.getString(69018) + loc2 + "：30 - " + loc3 + "：30");
                return false;
            }
            if (cache.killEvilTask.releaseInfo.canReleaseCount == 0) 
            {
                MsgManager.showRollTipsMsg(Language.getString(69019));
                return false;
            }
            if (cache.killEvilTask.getOwnNumByType(arg1) == 0) 
            {
                if (loc4 = this.getPropShopItemInfoByType(arg1)) 
                {
                    if (!this._quickBuy) 
                    {
                        this._quickBuy = QuickBuyWindow.instance;
                    }
                    this._quickBuy.update(loc4);
                    this._quickBuy.updateDefaultAmount(1);
                    this._quickBuy.show();
                }
                MsgManager.showRollTipsMsg(Language.getString(69020));
                return false;
            }
            return true;
        }

        /* internal  */getPropShopItemInfoByType(arg1: int): ShopItemInfo
        {
            var loc1=cache.killEvilTask.getKillEvilTaskColor(arg1);
            var loc2=ShopConfig.instance.getItemByType(ECategory._ECategoryProp, EProp._EPropKillEvilToken, -1, loc1);
            return loc2;
        }

        /* internal  */onMoudleShow(arg1: Object /* flash.events.Event */): void
        {
            if (this._killEvilTaskModule && !this._killEvilTaskModule.isHide) 
            {
                this._killEvilTaskModule.updateCanGetInfo();
                this._killEvilTaskModule.updateReleaseInfo();
                this._killEvilTaskModule.updateHadGetInfo();
            }
            return;
        }

        public onGetKillEvilTask(arg1: DataEvent): void
        {
            var loc1=arg1.data as int;
            if (!this.checkIsCanGetTask(loc1)) 
            {
                return;
            }
            this.onGetKillEvilTaskAlert(Alert.OK, loc1, false);
            return;
        }

        /* internal  */onGetKillEvilTaskAlert(arg1: int, arg2: int, arg3: Boolean): void
        {
            if (arg1 == Alert.OK) 
            {
                GameProxy.killEvilTask.getKillEvilTask(arg2);
            }
            if (arg3) 
            {
                PulseSharedObject.save(TodayNoTipsConst.KillEvilTaskGetDaily, true);
            }
            return;
        }

        /* internal  */checkIsCanGetTask(arg1: int): Boolean
        {
            var loc2=null;
            var loc3=null;
            var loc1=ClockManager.instance.nowDate;
            if ((loc1.hours < GameConst.GetKillEvilTaskTime_start || loc1.hours == GameConst.GetKillEvilTaskTime_start && loc1.minutes < 30 || loc1.hours > GameConst.GetKillEvilTaskTime_end || loc1.hours == GameConst.GetKillEvilTaskTime_end && loc1.minutes > 30) && !Global.isDebugModle) 
            {
                loc2 = GameConst.GetKillEvilTaskTime_start < 10 ? "0" + GameConst.GetKillEvilTaskTime_start : "" + GameConst.GetKillEvilTaskTime_start;
                loc3 = GameConst.GetKillEvilTaskTime_end < 10 ? "0" + GameConst.GetKillEvilTaskTime_end : "" + GameConst.GetKillEvilTaskTime_end;
                MsgManager.showRollTipsMsg(Language.getString(69027) + loc2 + "：30 - " + loc3 + "：30");
                return false;
            }
            if (cache.killEvilTask.myGetInfo.executeKillEvilType != 0) 
            {
                MsgManager.showRollTipsMsg(Language.getString(69028));
                return false;
            }
            if (cache.killEvilTask.myGetInfo.canGetCount == 0) 
            {
                MsgManager.showRollTipsMsg(Language.getString(69029));
                return false;
            }
            if (cache.killEvilTask.getReaseNumByType(arg1) == 0) 
            {
                MsgManager.showRollTipsMsg(Language.getString(69030));
                return false;
            }
            return true;
        }

        public onChangeCurrentPanel(arg1: DataEvent): void
        {
            this._killEvilTaskModule.setCurrentPanel(arg1.data as int);
            return;
        }

        /* internal  */doCommitKillTask(arg1: DataEvent): void
        {
            var loc2=null;
            var loc1=cache.task.getTaskByGroup(ETaskGroup._ETaskGroupKillEvil, ETaskStatus._ETaskStatusHadCompleted);
            if (loc1.length > 0) 
            {
                loc2 = loc1[0];
                GameProxy.taskProxy.endTaskReq(cache.dialog.npcInfo.tnpc.npcId, loc2.sTask.code);
            }
            return;
        }

        /* internal  */onGetKillTaskSuccess(arg1: DataEvent): void
        {
            return;
        }

        /* internal  */doQuickCommit(arg1: DataEvent): void
        {
            var loc2=null;
            var loc1=cache.task.getTaskByGroup(ETaskGroup._ETaskGroupKillEvil, ETaskStatus._ETaskStatusNotCompleted);
            if (loc1.length > 0) 
            {
                loc2 = loc1[0];
                Dispatcher.dispatchEvent(new DataEvent(EventName.TaskQuickComAndEndReq, loc2));
                flash.utils.setTimeout(this.onDelayHandler, 1000);
            }
            return;
        }

        /* internal  */onTaskUpdateHandler(arg1: Array<any>): void
        {
            if (_view && !_view.isHide) 
            {
                (view as IKillEvilTaskModule).updateHadGetInfo();
            }
            return;
        }

        /* internal  */onDelayHandler(): void
        {
            if (_view && !_view.isHide) 
            {
                GameProxy.killEvilTask.KillEvilCanGetInfo();
            }
            return;
        }

        /* internal  */onHintIconClick(arg1: DataEvent): void
        {
            var loc2=null;
            var loc3=0;
            var loc4=0;
            var loc5=null;
            var loc1=arg1.data as Array<any>;
            var loc6=0;
            var loc7=loc1;
            for(loc2 of loc7) 
            {
                loc3 = loc3 + loc2.issueNum;
                loc4 = loc4 + loc2.issueExp;
            }
            loc5 = Language.getStringByParam(69031, loc3, loc4);
            Alert.alertWinRenderer = CheckBoxWin;
            Alert.buttonWidth = 60;
            Alert.show(loc5, Language.getString(69002), Alert.OK, null, this.onHintIconAlert);
            return;
        }

        /* internal  */onHintIconAlert(arg1: int, arg2: Boolean): void
        {
            if (arg2) 
            {
                PulseSharedObject.save(TodayNoTipsConst.KillEvilTaskGotDaily, true);
            }
            return;
        }

        /* internal  */onReceiveReleaseInfo(arg1: MessageBlock): void
        {
            var loc1=arg1.messageBase as SKillEvilReleaseInfo;
            cache.killEvilTask.releaseInfo = loc1;
            if (this._killEvilTaskModule && !this._killEvilTaskModule.isHide) 
            {
                this._killEvilTaskModule.updateReleaseInfo();
                this._killEvilTaskModule.updateCanGetInfo();
            }
            return;
        }

        /* internal  */onReceiveCanGetInfo(arg1: MessageBlock): void
        {
            var loc1=arg1.messageBase as SKillEvilCanGetInfo;
            cache.killEvilTask.canGetInfo = loc1;
            if (this._killEvilTaskModule && !this._killEvilTaskModule.isHide) 
            {
                this._killEvilTaskModule.updateCanGetInfo();
            }
            return;
        }

        /* internal  */onReceiveMyKillInfo(arg1: MessageBlock): void
        {
            var loc1=arg1.messageBase as SMyKillEvilGetInfo;
            cache.killEvilTask.myGetInfo = loc1;
            if (this._killEvilTaskModule && !this._killEvilTaskModule.isHide) 
            {
                this._killEvilTaskModule.updateHadGetInfo();
            }
            return;
        }

        /* internal  */onKillTaskIssue(arg1: MessageBlock): void
        {
            var loc2=null;
            var loc1=PulseSharedObject.isTodayNotTips(TodayNoTipsConst.KillEvilTaskGotDaily);
            if (!loc1) 
            {
                loc2 = arg1.messageBase as SSystemGetTaskIssue;
                if (!this._hinticon) 
                {
                    this._hinticon = new KillEvilTaskHintIcon();
                }
                this._hinticon.addInfo(loc2);
                this._hinticon.show();
            }
            return;
        }

        /* internal  */serverBagDataChangehandler(arg1: Object=null): void
        {
            if (this._killEvilTaskModule && !this._killEvilTaskModule.isHide) 
            {
                this._killEvilTaskModule.updateReleaseInfo();
            }
            return;
        }

        /* internal  */onCompleteKillEvilTask(arg1: MessageBlock): void
        {
            return;
        }

        protected /* override */ initView(): IView
        {
            if (!this._killEvilTaskModule) 
            {
                this._killEvilTaskModule = new KillEvilTaskModule();
                this._killEvilTaskModule.addEventListener(WindowEvent.SHOW, this.onMoudleShow);
                this._killEvilTaskModule.addEventListener(GModuleEvent.KillEvilTaskGetTask, this.onGetKillEvilTask);
                this._killEvilTaskModule.addEventListener(GModuleEvent.KillEvilTaskQuickCommit, this.doQuickCommit);
                this._killEvilTaskModule.addEventListener(GModuleEvent.KillEvilTaskChangeCurrentPanel, this.onChangeCurrentPanel);
                this._killEvilTaskModule.addEventListener(GModuleEvent.KillEvilTaskCommit, this.doCommitKillTask);
            }
            return this._killEvilTaskModule;
        }

        protected /* override */ initServer(): void
        {
            Dispatcher.addEventListener(EventName.KillEvilTaskOpenTaskWin, this.onOpenKillEvilTaskWin);
            Dispatcher.addEventListener(EventName.KillEvilTaskUsePropFromPack, this.onKillEvilTaskPropUse);
            Dispatcher.addEventListener(EventName.KillEvilTaskPublishTask, this.onPublishKillEvilTask);
            Dispatcher.addEventListener(EventName.KillEvilTaskGetTaskSuccess, this.onGetKillTaskSuccess);
            Dispatcher.addEventListener(KillEvilTaskHintIcon.KillEvilHintIconClick, this.onHintIconClick);
            NetDispatcher.addCmdListener(EGateCommand._ECmdGateKillEvilReleaseInfo, this.onReceiveReleaseInfo);
            NetDispatcher.addCmdListener(EGateCommand._ECmdGateKillEvilCanGetInfo, this.onReceiveCanGetInfo);
            NetDispatcher.addCmdListener(EGateCommand._ECmdGateMyKillEvilGetInfo, this.onReceiveMyKillInfo);
            NetDispatcher.addCmdListener(EGateCommand._ECmdGateSystemGetTaskIssue, this.onKillTaskIssue);
            NetDispatcher.addCmdListener(EGateCommand._ECmdGateCompleteKillEvilTask, this.onCompleteKillEvilTask);
            NetDispatcher.addCmdListener(ServerCommand.BackpackDataChange, this.serverBagDataChangehandler);
            NetDispatcher.addCmdListener(ServerCommand.BackPackItemsChange, this.serverBagDataChangehandler);
            NetDispatcher.addCmdListener(ServerCommand.TaskUpdate, this.onTaskUpdateHandler);
            return;
        }

        private /* var */_hinticon: KillEvilTaskHintIcon;

        private /* var */_killEvilTaskModule: IKillEvilTaskModule;

        private /* var */_quickBuy: QuickBuyWindow;
    }
